
//--> Loth 2011 
//--> Flare3D engine & interface init
//--> link: http://www.flare3d.com/

package chamans.engine
{
	import chamans.*;
	
	import flare.basic.*;
	import flare.core.*;
	import flare.loaders.collada.*;
	import flare.primitives.*;
	import flare.system.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.ui.Keyboard;
	
	public class Flare3d extends Sprite
	{
		public static var stage:Stage;
		public var FlashLimiteVersion:Array=[10, 0, 3, 181];
		public var physicEngine:String;
		public var Engine:String;
		public var act:Boolean=false;
		// chamans 
		public var KEY:key;
		public var LOAD:load;
		public var TOOL:Tool;
		public var SYS:Interface;
		public var PAUSE:PauseScreen;
		// init variable
		private var END:Function;
		private var LOOP:Function;
		private var def:Object;
		// basic variable ?
		public var autoMove:Boolean=false;
		public var mouseHide:Boolean=false;
		public var MenuActif:Boolean=false;
		// resize variable
		public var limite:Boolean=true;
		public var size:Point=new Point(0,0);
		public var sizelimit:Point=new Point(800,450);
		private var TMP:Function;
		//______________________o
		// Flare3d 2.0 variable
		public var scene:Viewer3D;
		public var FogColor:int;
		public var FogDistance:int;
		
		//_______________________________________________loop function
		private function OO(event:Event=null):void
		{
			if(LOOP!=null)LOOP();
			//GETKEYS();
		}
		public function Flare3d (end:Function=null, loop:Function=null, setting:Object=null)
		{
			END=end;
			LOOP=loop;
			def = setting || new Object();
			if(stage) init(); // > flash builder
			else addEventListener(Event.ADDED_TO_STAGE,onStage);// > flash cs5
		}
		private function onStage(e:Event):void
		{
			removeEventListener(Event.ADDED_TO_STAGE,onStage);
			init();
		}
		private function init():void
		{
			// chamans usefull utils
			LOAD = new load(); TOOL = new Tool(); KEY = new key(stage);
			// stage set
			with (stage){ align = "TL"; scaleMode = "noScale"; quality = "HIGH"; }
			init_2D();
		}
		//_______________________________________________2D interface init
		private function init_2D():void
		{
			SYS = new Interface( this, init_VIEW3D, def);
			addChild(SYS)
		}
		//_______________________________________________3d Engine init
		private function init_VIEW3D():void
		{
			ColladaLoader.texturesFolder = "assets/flare3d/textures"; // <--needed for collada.
			
			scene = new Viewer3D( this );	
			scene.setViewport( 0, 0, stage.stageWidth, stage.stageHeight );
			scene.camera.setPosition( 0, 150,  -150 );
			scene.camera.lookAt( 0, 0, 0 );
			scene.camera.far = 100000;
			// light parameters
			scene.ambientColor = new Vector3D( 0.2, 0.2, 0.2 );  // <-- RGB ambient light color.
			scene.defaultLight.color = new Vector3D( 1, 1, 1, 1 ); // <-- RGB light colors and contrast.
			scene.defaultLight.setRotation( 40, 40, 0 );
			Active();
	
			FogDistance = def.FogDistance || 1000;
			FogColor = def.FogColor || 0x413A2E;
			if(END!=null)END();
		}
		//____________________________________________flare3d base function
		public function ADD(link:String, end:Function):Pivot3D
		{
			scene.addEventListener( Scene3D.PROGRESS_EVENT, progressEvent );
			scene.addEventListener( Scene3D.COMPLETE_EVENT, completeEvent );
			TMP=end;
			return scene.addChildFromFile( link );
		}
		public function CLONE(obj:Pivot3D):Pivot3D
		{
			var cloned:Pivot3D = obj.clone();
			scene.addChild( cloned );
			return cloned;
		// change some properties.
		//	cloned.setPosition( 25, 0, 80 )
		//	cloned.frameSpeed = -0.5       
		//	cloned.gotoAndPlay( 80 )
		}
		private function progressEvent(e:Event):void 
		{
			// at the moment, the value it not presice if it has textures.
			// we are using the same "scene.resources" context for all files now.
			// first we load the DAE file, then all textures.
			SYS.OUT(scene.loadProgress.toString())
		}
		
		private function completeEvent(e:Event):void 
		{
		 	SYS.OUT(SYS.MESSAGE)
				TMP()
		}
		//_______________________________________________rezise
		public function RESIZE(e:Event=null):void
		{
			size=new Point( stage.stageWidth, stage.stageHeight);
			SYS.OUT("resize "+size.x+" "+size.y);
			if(SYS.actif) SYS.cadre(size);
			if(PAUSE) PAUSE.Resize();
			if(scene) RESIZE3D();
			if(limite && size.x==sizelimit.x && size.y==sizelimit.y)activeResize(false);
		}
		public function RESIZE3D(e:Event=null):void
		{
			scene.setViewport( 0, 0, size.x, size.y );
		}
		public function activeResize(n:Boolean=true):void 
		{
			if(n)stage.addEventListener('resize',  RESIZE, false, 0, true);
			else {stage.removeEventListener('resize',  RESIZE); 
			SYS.OUT(SYS.MESSAGE);
		}}
		//_______________________________________________Pause screen
		private function Active(e:Event=null):void
		{
			stage.removeEventListener(Event.ACTIVATE,Active);
			stage.addEventListener(Event.ENTER_FRAME, OO, false, 0, true);
			stage.addEventListener(Event.DEACTIVATE,Desactive, false, 0, true);
			if(PAUSE!=null){PAUSE.clear(); removeChild(PAUSE);PAUSE=null}
		}
		private function Desactive(e:Event):void 
		{
			stage.removeEventListener(Event.ENTER_FRAME, OO);
			stage.removeEventListener(Event.DEACTIVATE,Desactive);
			stage.addEventListener(Event.ACTIVATE,Active, false, 0, true);
			if(PAUSE==null) PAUSE=new PauseScreen(this); addChild(PAUSE);
		}	
}}